require(['./config'], () => {
    require(['request', 'footer'], (request) => {
       class Longin {
            constructor() {
                this.login()
                this.register()
            }
            
            //登录
            login() {
                //登录回调函数
                function loginfn(resp) {
                    if(resp.code === 200) {
                        localStorage.setItem('user', JSON.stringify(resp.body))
                        location.replace('/index.html')
                    } else {
                        alert('账号或密码错误')
                    }
                }
                $('#logbtn').on('click', () => {
                    let username = $('#username').val()
                    let password = $('#psw').val()
                    var userInfo = {username,password}
                    // request.login().then(resp => {
                    //     // if(resp.code === 200) {
                    //     //     console.log(1)
                    //     // } else {
                    //     //     console.log(resp)
                    //     // }
                    //     const islog = resp.some((item, index) => {
                    //         if(item.username === username && item.password === password) {
                    //             this.userInfo = item
                    //             return true
                    //         } else {
                    //             return false
                    //         }
                    //     })
                    //     if(islog) {
                    //         localStorage.setItem('user', JSON.stringify(this.userInfo))
                    //         location.replace('/index.html')
                    //     } else {
                    //         alert('账户或密码错误!,请重新输入')
                    //     }
                    // })
                    request.login(userInfo).then(resp => {
                        resp = JSON.parse(resp)
                        if(resp.code == 200) {
                            console.log(1)
                            localStorage.setItem('user', JSON.stringify(resp.body))
                            alert('登录成功')
                            location.replace('/index.html')
                        } else {
                            console.log(2)
                        }
                        console.log(resp)
                    })
                })
            }

            // 注册 
            register() {
                $('#resbtn').on('click', () => {
                    let username = $('#username').val()
                    let password = $('#psw').val()
                    var reg = /^\D{1}\w{5,11}$/
                    var isuser = reg.test(username)
                    var ispsw = reg.test(password)
                    var userInfo = {username,password}
                    if(isuser && ispsw) {
                        request.login(userInfo).then(resp => {
                            resp = JSON.parse(resp)
                            if(resp.code === 200) {
                                alert('账号重复，请重新输入')
                            } else {
                                request.register(userInfo).then(resp => {
                                    resp = JSON.parse(resp)
                                    if(resp.code === 200) {
                                        localStorage.setItem('user', JSON.stringify(userInfo))
                                        alert('注册成功')
                                        location.replace('/index.html')
                                    } else {
                                        alert("注册失败")
                                    }
                                    
                                })
                            }
                        })
                    } else {
                        alert("账号和密码均需6-12位数字字母或下划线而且不能以数字开头")
                    }
                })
            }
       }
       new Longin()
    })
})